
package com.compuvest.web;

// <editor-fold defaultstate="collapsed" desc="import">
import java.io.Serializable;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
// </editor-fold>

public class CookieBean implements Serializable {

// <editor-fold defaultstate="collapsed" desc="init">
    public final static int MAX_AGE = 2592000; // 2592000; 86400 - 1 day;  * 30 = 1 month
//    public final static String MY_DOMAIN = ".compuvestb2b.com";

    private String cjpid;
    private String cjaid;
    private String cjsid;
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="CookieBean()">

    public CookieBean(HttpServletRequest request, HttpServletResponse response) {

        Cookie[] cookies = request.getCookies();
        if(cookies == null) { // make new cookies for CJ
//            System.out.println("New Cookie");

//            Cookie sess_id = new Cookie("JSESSIONID", request.getSession().getId());
//            sess_id.setPath("/");
//            sess_id.setDomain(MY_DOMAIN);
//            sess_id.setMaxAge(MAX_AGE);
//            response.addCookie(sess_id);

            String advsite = request.getParameter("advsite");
            if(advsite != null && "onetime".equals(advsite)) {
                Cookie sess_adv = new Cookie("advert", advsite);
                sess_adv.setPath("/");
//                sess_adv.setDomain(MY_DOMAIN);
                sess_adv.setMaxAge(MAX_AGE);
                response.addCookie(sess_adv);
                cjpid = (String)request.getParameter("CJPID");
                if(cjpid != null) {
                    Cookie ccjpid = new Cookie("CJPID", cjpid);
                    ccjpid.setPath("/");
//                    ccjpid.setDomain(MY_DOMAIN);
                    ccjpid.setMaxAge(MAX_AGE);
                    response.addCookie(ccjpid);
                }
                cjaid = (String)request.getParameter("CJAID");
                if(cjaid != null) {
                    Cookie ccjaid = new Cookie("CJAID", cjaid);
                    ccjaid.setPath("/");
//                    ccjaid.setDomain(MY_DOMAIN);
                    ccjaid.setMaxAge(MAX_AGE);
                    response.addCookie(ccjaid);
                }
                cjsid = (String)request.getParameter("CJSID");
                if(cjsid != null) {
                    Cookie ccjsid = new Cookie("CJSID", cjsid);
                    ccjsid.setPath("/");
//                    ccjsid.setDomain(MY_DOMAIN);
                    ccjsid.setMaxAge(MAX_AGE);
                    response.addCookie(ccjsid);
                }
            } // if
        }
        else { // if find CJ params - store to order
//            System.out.println("Old Cookie ");
            for(int i = 0; i < cookies.length; i++) {
                switch(cookies[i].getName()) {
                    case "CJPID":
                        cjpid = cookies[i].getValue();
                        break;
                    case "CJAID":
                        cjaid = cookies[i].getValue();
                        break;
                    case "CJSID":
                        cjsid = cookies[i].getValue();
                        break;
                }
            } // for
        } //else

    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="getCJ[]()">

    public String getCJPid()	{ return cjpid; }
    public String getCJAid()	{ return cjaid; }
    public String getCJSid()	{ return cjsid; }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="toString">

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();

        sb.append("\n\tcjpid: ").append(cjpid);
        sb.append("\n\tcjaid: ").append(cjaid);
        sb.append("\n\tcjsid: ").append(cjsid);

        return sb.toString();
    }
// </editor-fold>

}
